package cn.kgc.novel.controller;

import cn.kgc.novel.common.Result;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.ServerSocket;
import java.net.Socket;

@Controller
public class ServerController {
    private ServerSocket ss = null;
    private Socket socket = null;
    @RequestMapping("/server")
    @ResponseBody
    public void server(){
        try {
            ss = new ServerSocket(9001);
            while(true){
                socket = ss.accept();
                System.out.println("成功建立socket连接");
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @RequestMapping("/getmsg")
    @ResponseBody
    public Result getmsg(){
        try{
            while(true){
                System.out.println("接收到socket");
                BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(socket.getInputStream()));
                String msg;
                while((msg=bufferedReader.readLine())!=null){
                    System.out.println("进入到了读中");
                    System.out.println("读到的结果："+msg);

                }
                return  Result.ok(msg);
            }
        }catch (Exception e){
            e.printStackTrace();
            return null;
        }
    }

}
